define(['jquery', 'bootstrap', 'backend', 'table', 'form','bootstrap-datetimepicker'], function ($, undefined, Backend, Table, Form,datetimepicker) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            $(".btn-add").data("area",["1500px","90%"]);
            $(".btn-edit").data("area",["1500px","90%"]);
            Table.api.init({
                extend: {
                    index_url: 'order/purchase/index' + location.search,
                    add_url: 'order/purchase/add',
                    edit_url: 'order/purchase/edit',
/*
                    del_url: 'order/purchase/del',
*/
                    multi_url: 'order/purchase/multi',
                    import_url: 'migrate/import_bin_sell',
                    table: 'order_purchase',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                fixedColumns: true,
                searchFormVisible: true,
                fixedRightNumber: 4,


                /*
                                fixedRightNumber: 1,
                */
                columns: [
                    [
                        {checkbox: true},
/*
                        {field: 'id', title: __('Id')},
*/
                        {field: 'take_number', title: __('Take_number'), operate: 'LIKE'},


                        {field: 'purchasetime', title: __('Purchasetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'yard.organ_name', title: __('存放堆场'), operate: 'LIKE'},
                        {field: 'bincompany.organ_name', title: __('箱源公司'), operate: 'LIKE'},
                        {field: 'ourcompany.organ_name', title: __('我方公司'), operate: 'LIKE'},
                        {field: 'purchase_number', title: __('Purchase_number'), operate: false},
                        {field: 'complete_number', title: __('完成数量'), operate: false},
                        {field: 'type.name', title: __('Bin_type'), searchList: $.getJSON('bin.type/searchlist'),},
                        {field: 'purchase_type', title: __('Purchase_type'), searchList: {"1":__('Purchase_type 1'),"2":__('Purchase_type 2')}, formatter: Table.api.formatter.normal},
                        {field: 'admin.nickname', title: __('Salesman_id'), operate: 'LIKE'},
                        {field: 'native.name', title: __('Bin_native'),searchList: $.getJSON('bin.native/searchlist'), formatter: Table.api.formatter.normal},
                        {field: 'unit_price', title: __('Unit_price'), operate:'BETWEEN'},
                        {field: 'currency_type', title: __('Currency_type'), searchList: {"1":__('Currency_type 1'),"2":__('Currency_type 2'),"3":__('Currency_type 3'),"4":__('Currency_type 4')}, formatter: Table.api.formatter.status},
                        {field: 'is_finish', title: __('Is_finish'), searchList: {"0":__('Is_finish 0'),"1":__('Is_finish 1')}, formatter: Table.api.formatter.normal},

                        /*

                       {field: 'weigh', title: __('Weigh'), operate: false},
*/
                        {field: 'purchase_sn', title: __('买箱单号'), operate: 'LIKE'},

                        {field: 'createtime', title: __('Createtime'), operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'last_remark', title: __('最后备注'),  align: 'left',operate: false},


                        {
                            field: 'remark',
                            width: "100px",
                            title: '备注',
                            table: table,
                            formatter: Table.api.formatter.buttons,
                            buttons: [

                                {
                                    name: 'addtabs',
                                    title: __('查看全部备注'),
                                    text:  function(row,value){

                                        return '备注('+row.remark_total_nuber+')';
                                        /*
                                                                                return '备注';
                                        */


                                    },
                                    classname: 'btn btn-xs btn-warning btn-dialog',
                                    icon: 'fa fa-folder-o',
                                    url: function(row,value){
                                        return "remark/remark_list/correspond_id/"+row.id+'/type/1';
                                    }
                                },
                            ]
                        },
                        {
                            field: 'buttons',
                            width: "120px",
                            title: __('箱子列表'),
                            table: table,
                            operate: false,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'addtabs',
                                    text: function(row,value){

                                        return '查看('+row.complete_number+')';


                                    },
                                    extend:'data-area=\'["900px","100%"]\' data-shade=\'[0.5,"#000"]\'',
                                    title: '箱子列表',
                                    classname: 'btn btn-xs btn-info btn-addtabs',
                                    icon: 'fa fa-folder-o',
                                    url: function(row,value){
                                        // 假设URL是 http://example.com/page?name=John&age=30

                                        return "bin/index/origin_sn/"+row.purchase_sn;
                                    },
                                    hidden:function (row){
                                        /*  if(row.current_status==2){
                                              return true;
                                          }*/
                                    },
                                },
                            ],
                            formatter: Table.api.formatter.buttons
                        },
                        {
                            field: 'buttons',
                            width: "120px",
                            title: __('去入库'),
                            table: table,
                            operate: false,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'addtabs',
                                    text: '添加箱子',
                                    extend:'data-area=\'["1500px","90%"]\' data-shade=\'[0.5,"#000"]\'',
                                    title: '去入库',
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    icon: 'fa fa-plus',
                                    url:function (row,value){
                                       var purchase_type= Fast.api.query("purchase_type");
                                        return   "order.purchase/add_bin/ids/"+row.id+"/purchase_type/"+row.purchase_type;
                                    },
                                    hidden:function (row){
                                          if(row.is_finish==1){
                                              return true;
                                          }
                                    },
                                },
                                {
                                    name: 'detail',
                                    text: __('已完成'),
                                    title: __('已完成'),
                                    classname: 'btn btn-xs btn-danger',

                                    hidden: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if(row.is_finish!=1){
                                            return true;
                                        }

                                    }
                                },
                            ],
                            formatter: Table.api.formatter.buttons
                        },
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}


                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            table.on('post-body.bs.table',function(){
                $(".btn-editone").data("area",["1500px","90%"]);
            });
        },



        add: function () {

            $(document).on("click", "#bin-operator", function () {
              var maxClicks= $('#c-purchase_number').val();
              var binsize = $("[fieldlist-item='true']").size();
              var bin_type = $("#c-bin_type").val();
              var bin_native = $("#c-bin_native").val();
              var yard_id = $("#c-yard_id").val();
              var take_number = $("#c-take_number").val();
              var purchasetime = $("#c-purchasetime").val();

               setInterval(function(){
                  if (binsize < maxClicks) {
                      binsize++;
/*
                      console.log('连续点击次数: ' + binsize);
*/
                      $('.btn-append').trigger("click");
                      $('.bin_type').val(bin_type);
                      $('.bin_native').val(bin_native);
                      $('.yard_id').val(yard_id);
                      $('.entertime').val(purchasetime);
                      $('.take_number').val(take_number);
                      $('.datetimepicker').parent().css('position', 'relative');
                      $('.datetimepicker').datetimepicker({
                          format:'YYYY-MM-DD HH:mm:ss',
                      });

                  }
               }, 1);

           });

            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        rent_add: function () {
            $(document).on("click", "#bin-operator", function () {
                var maxClicks= $('#c-purchase_number').val();
                var binsize = $("[fieldlist-item='true']").size();
                var bin_type = $("#c-bin_type").val();
                var bin_native = $("#c-bin_native").val();
                var yard_id = $("#c-yard_id").val();
                var take_number = $("#c-take_number").val();
                var purchasetime = $("#c-purchasetime").val();


                setInterval(function(){
                    if (binsize < maxClicks) {
                        binsize++;
                        /*
                                              console.log('连续点击次数: ' + binsize);
                        */
                        $('.btn-append').trigger("click");
                        $('.bin_type').val(bin_type);
                        $('.bin_native').val(bin_native);
                        $('.yard_id').val(yard_id);
                        $('.entertime').val(purchasetime);
                        $('.take_number').val(take_number);
                        $('.datetimepicker').parent().css('position', 'relative');
                        $('.datetimepicker').datetimepicker({
                            format:'YYYY-MM-DD HH:mm:ss',
                        });
                    }
                }, 1);

            });
            Controller.api.bindevent();
        },
        add_bin: function (){
            $(document).on("click", "#btn-tianjia", function () {
                $('.btn-append').trigger("click");

                $('.bin_type').val( $("#c-bin_type").val());
                $('.bin_native').val( $("#c-bin_native").val());
                $('.yard_id').val( $("#c-yard_id").val());
                $('.take_number').val( $("#c-take_number").val());
                $('.datetimepicker').parent().css('position', 'relative');
                $('.datetimepicker').datetimepicker({
                    format:'YYYY-MM-DD HH:mm:ss',
                });
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));

            }
        }
    };
    return Controller;
});

$(document).on('click','.spec_add_btn', function (event) {
    var url = $(this).attr('data-url');
    if(!url) return false;
    var msg = $(this).attr('data-title');
    var width = $(this).attr('data-width');
    var height = $(this).attr('data-height');

    var options = {
        shadeClose: false,
        shade: [0.3, '#393D49'],
        area: ["1800px","90%"],
        callback:function(value){
            CallBackFun(value.id, value.name);//在回调函数里可以调用你的业务代码实现前端的各种逻辑和效果
        }
    };
    Fast.api.open(url,msg,options);
});
